using System;
using System.CodeDom.Compiler;
using System.IO;
using System.Windows.Forms;
using ArcEngine.Resources;
using WeifenLuo.WinFormsUI.Docking;


namespace ArcEngine.Editor
{
	public partial class ScriptForm : DockContent
	{
		public ScriptForm()
		{
			InitializeComponent();


		}



		/// <summary>
		/// 
		/// </summary>
		/// <param name="list"></param>
		/// <returns></returns>
		public bool Init(Script script)
		{
			Script = script;


			if (Script != null)
				ScriptTxt.Text = Script.SourceCode;


			//
			PropertyBox.SelectedObject = Script;
			TabText = Script.Name;


			return true;
		}




		#region Variables

		Script Script;

		#endregion


		/// <summary>
		/// Saves the source code of the script
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SaveButton_OnClick(object sender, EventArgs e)
		{

			if (Script != null)
			{
				Script.SourceCode = ScriptTxt.Text;
			}
		}


		/// <summary>
		/// Compiles the script
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void Compile_OnClick(object sender, EventArgs e)
		{

			if (Script != null)
			{
				Log.Send(new LogEventArgs(LogLevel.Info, "Compiling " + Script.Name + "...", null));

				ErrorListView.Items.Clear();

				Script.SourceCode = ScriptTxt.Text;
				if (!Script.Compile(true))
				{

					ListViewItem l;

					// Add each error as a listview item with its line number
					foreach (CompilerError err in Script.Errors)
					{
						l = new ListViewItem(err.ErrorText);
						l.SubItems.Add(err.Line.ToString());
						ErrorListView.Items.Add(l);
					}
				}

				string txt = "Compile complete -- ";
				if (Script.HasErrors)
				{
					txt += Script.Errors.Count + " error(s).";
					LogStatusLabel.Text = Script.Errors.Count + " error(s).";
				}
				else
				{
					txt += " 0 error.";
					LogStatusLabel.Text = "0 error.";
				}

				Log.Send(new LogEventArgs(LogLevel.Info, txt, null));



			}
		}


		/// <summary>
		/// Double click on the error bring on the error's line
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ListViewError_OnItemActive(object sender, EventArgs e)
		{
			return;

			int l = Convert.ToInt32(ErrorListView.SelectedItems[0].SubItems[1].Text);
			int i, pos;

			if (l != 0)
			{
				i = 1;
				pos = 0;
				while (i < l)
				{
					pos = ScriptTxt.Text.IndexOf(Environment.NewLine, pos + 1);
					i++;
				}
				ScriptTxt.SelectionStart = pos;
				ScriptTxt.SelectionLength = ScriptTxt.Text.IndexOf(Environment.NewLine, pos + 1) - pos;
			}

			ScriptTxt.Focus();

		}


		/// <summary>
		/// Insert IEntity model script
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void InsertIEntityButton_Click(object sender, EventArgs e)
		{
			ScriptTxt.Text = @"using System;
using System.Drawing;
using ArcEngine;
using ArcEngine.Audio;
using ArcEngine.Core;
using ArcEngine.Graphics;
using ArcEngine.Input;
using ArcEngine.Network;
using ArcEngine.Resources;
using ArcEngine.Time;


public class entity : IEntity
{
	///
	/// Handle to the entity
	///
	Entity Entity;

	/// <summary>
	/// 
	/// </summary>
	/// <param name=""entity""></param>
	public bool Init(Entity entity)
	{
		Entity = entity;
		return true;
	}

	/// <summary>
	/// 
	/// </summary>
	/// <param name=""elapsed""></param>
	public void Update(int elapsed)
	{
	}
}


";
		}


		/// <summary>
		/// Load a script from a file
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void LoadButton_Click(object sender, EventArgs e)
		{
			OpenFileDialog dlg = new OpenFileDialog();
			dlg.Filter = "Script file (*.cs; *.txt; *.vb)|*.cs;*.txt;*.vb|All Files (*.*)|*.*";
			dlg.Title = "Select script file to open...";
			dlg.DefaultExt = ".cs";

			DialogResult res = dlg.ShowDialog();
			if (res != DialogResult.OK)
				return;

			// Open the stream and read it back.
			if (File.Exists(dlg.FileName))
			{
				string s = "";
				using (StreamReader sr = File.OpenText(dlg.FileName))
				{
					while (!sr.EndOfStream)
						s += sr.ReadLine() + Environment.NewLine;

					ScriptTxt.Text = s;
				}
			}

			// Back in the good directory
			Environment.CurrentDirectory = Application.StartupPath;

		}



		/// <summary>
		/// Text changed
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ScriptTxt_TextChanged(object sender, EventArgs e)
		{
			Script.SourceCode = ScriptTxt.Text;
		}


		/// <summary>
		/// Save the script to the resource
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void ToolSave_Click(object sender, EventArgs e)
		{
			Script.SourceCode = ScriptTxt.Text;
		}





	}
}
